"use strict";
/* Controllers */
app.controller("wordsTopController", ['$scope', '$location', '$rootScope', '$routeParams', 'wordTopService', function ($scope, $location, $rootScope, $routeParams, wordTopService) {

    var vm = $scope.vm;
    vm.p = {};
    //vm.p.appID = $routeParams.appID; //  获取应用模块的名称

    var errorFn = function (result) {
        alert(result);
    };

    $scope.listWordTop = function () {
        var vm = $scope.vm;
        var params = {};
        params = vm.p;
        $('#wuba_table').bootstrapTable('showLoading');
        $('#ganji_table').bootstrapTable('showLoading');
        $("#refreshTable").addClass('disabled');
        var procFn = function (response) {
            var data = response.data;
            if (data == null) return;

            $scope.areaNames = data.areaNames;

            $('#wuba_table').bootstrapTable('load', data.wuba);
            $('#ganji_table').bootstrapTable('load', data.ganji);

            $('#wuba_table').bootstrapTable('hideLoading');
            $('#ganji_table').bootstrapTable('hideLoading');

            $("#refreshTable").removeClass('disabled');
        };
        wordTopService.list(params, procFn, errorFn);
    };

    $scope.clearSearch = function () {
        var vm = $scope.vm;
        vm.p.searchType = '';
        vm.p.endpoint = '';
        vm.p.areaName = '';
    };

    $scope.initializeController = function () {
        var $resultTable = $('#wuba_table');
        $resultTable.bootstrapTable({
             pageList: [30, 100, 1000], pageSize: 30
        });

        var $resultTable2 = $('#ganji_table');
        $resultTable2.bootstrapTable({
             pageList: [30, 100, 1000], pageSize: 30
        });

        $resultTable.bootstrapTable('hideColumn', 'costHjzw');
        $resultTable.bootstrapTable('hideColumn', 'costYouxuan');
        $resultTable.bootstrapTable('hideColumn', 'resumeDeliveryNum');
        $resultTable.bootstrapTable('hideColumn', 'imNum');
        $resultTable.bootstrapTable('hideColumn', 'telConnNum');

        $resultTable2.bootstrapTable('hideColumn', 'costHjzw');
        $resultTable2.bootstrapTable('hideColumn', 'costYouxuan');
        $resultTable2.bootstrapTable('hideColumn', 'resumeDeliveryNum');
        $resultTable2.bootstrapTable('hideColumn', 'imNum');
        $resultTable2.bootstrapTable('hideColumn', 'telConnNum');


        $scope.listWordTop();
    };

    $scope.initializeController();

    $scope.searchWordTop = function () {
        $('#list_table').bootstrapTable('refresh');
        $scope.listWordTop()
    };
    $scope.toggleInteval = function (interval) {
        $scope.vm.p.interval = interval;
        $scope.searchWordTop()
    }

}]);

function toThousands(num, row, index) {
    var result = '', counter = 0;
    num = (num || 0).toString();
    for (var i = num.length - 1; i >= 0; i--) {
        counter++;
        result = num.charAt(i) + result;
        if (!(counter % 3) && i != 0) { result = ',' + result; }
    }
    return result;
}

function toPencent(num, row, index) {
    if (num == null) {
        return "-";
    }
    return (parseFloat(num)*100).toFixed(2) + '%'
}


/* Services */
app.service('wordTopService', ['ajaxService', function (ajaxService) {

    this.list = function (params, successFunction, errorFunction) {
        ajaxService.AjaxPost(params, "/star/api/basic-service/v1/wordTop/list.do", successFunction, errorFunction);
    };

}]);

function wubaWordFormat(value, row, index) {
    return wordFormat(value, 'wuba')
}

function ganjiWordFormat(value, row, index) {
    return wordFormat(value, 'ganji')
}

function wordFormat(value, business) {
    if (value == null) {
        return "-";
    }
    //通过controller来获取Angular应用
    var appElement = document.querySelector('[ng-controller=index]');
    //获取$scope变量
    var $scope = angular.element(appElement).scope();
    $scope.vm.p.business = business;

    var urlParam = {}
    urlParam.inword = value
    urlParam.business = business
    if ($scope.vm.p.catename != '' && typeof ($scope.vm.p.catename) != 'undefined' && $scope.vm.p.catename.indexOf("/") >= 0) {
        urlParam.catename = $scope.vm.p.catename.replaceAll('/', '|')
        console.log(urlParam)
    } else {
        urlParam.catename = $scope.vm.p.catename
    }
    urlParam.areaName = $scope.vm.p.areaName
    urlParam.endpoint = $scope.vm.p.endpoint
    urlParam.searchType = $scope.vm.p.searchType


    // var str='<a href="#/star/main/words_chart' + value + '" target="_blank">' + value + '</a>'    
    var str = '<a href="#/star/main/words_chart/' + encodeURIComponent(JSON.stringify(urlParam)) + '" target="_blank">' + value + '</a>';
    return str;

}

// /** 
//  * param 将要转为URL参数字符串x的对象 
//  * key URL参数字符串的前缀 
//  * encode true/false 是否进行URL编码,默认为true 
//  *  
//  * return URL参数字符串 
//  */
// var urlEncode = function (param, key, encode) {
//     if (param == null) return '';
//     var paramStr = '';
//     var t = typeof (param);
//     if (t == 'string' || t == 'number' || t == 'boolean') {
//         paramStr += '&' + key + '=' + ((encode == null || encode) ? encodeURIComponent(param) : param);
//     } else {
//         for (var i in param) {
//             var k = key == null ? i : key + (param instanceof Array ? '[' + i + ']' : '.' + i);
//             paramStr += urlEncode(param[i], k, encode);
//         }
//     }
//     return paramStr;
// };




